class PartnershipsController < ApplicationController
  before_filter :find_partnership, :only => [:show, :edit, :update]

  def index
    @partnerships = Partnership.find(:all).paginate(:page => params[:page], :per_page => 6)
  end

  def show
  end

  def new
    @partnership = Partnership.new
  end

  def create          
    @partnership = Partnership.new(params[:partnership])
    @partnership.save!
    respond_to do |wants|
      wants.html {}
      wants.js   {
        head :ok
      }
    end
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    render :action => 'new'
  end

  def edit
  end

  def update
    @partnership.update_attributes!(params[:partnership])
    flash[:update] = 'The information for this partnership has been updated.'      
    redirect_to user_url
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    render :action => 'edit'
  end

  def destroy_by_item
    if params[:item_type] == 'photo'
      @partnership = Partnership.find(:first, :conditions => ["photo_id = ? and gallery_id = ?", params[:partnership][:item_id], params[:partnership][:gallery_id]] )
    else
      @partnership = Partnership.find(:first, :conditions => ["video_id = ? and gallery_id = ?", params[:partnership][:item_id], params[:partnership][:gallery_id]] )
    end
    
    @partnership.destroy
    respond_to do |wants|
      wants.js { head :ok}
    end
  end

protected
  def find_partnership
    @partnership = Partnership.find params[:id]
  end
end
